package com.xm.xmds1705083.web.controller;

import com.xm.xm.dto.ResultDto;
import com.xm.xm.dto.SearchDto;
import com.xm.xm.util.ErrorCode;
import com.xm.xmds1705083.service.service.DuoduoUserSpaceService;
import com.xm.xmds1705083.web.base.BaseController;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

/**
 * 杭州新苗网络科技有限公司
 *
 * @author meihf
 * @create 2017/6/16
 * @description
 */
@Controller
@RequestMapping("/userSpace")
public class UserSpaceController extends BaseController{

    @Autowired
     DuoduoUserSpaceService duoduoUserSpaceService;

    /**
     * 认证车位
     * @param searchDto
     * @return
     */
   @ResponseBody
    @RequestMapping("/certfMyParking")
    public ResultDto certfMyParking(@RequestBody SearchDto searchDto){
        /*searchDto.setAppUserId(getUserId());*/
       String authorization =   getRequest().getHeader("AUTHORIZATION");
       if (authorization!=null){
           searchDto.setAppUserId(Math.abs( Long.valueOf( authorization.split("\\_")[0] ) ));
       }
        return duoduoUserSpaceService.certfMyParking(searchDto);
    }
    /**
     * 认证身份证
     * @param searchDto
     * @return
     */
    @ResponseBody
    @RequestMapping("/certfMyName")
    public ResultDto certfMyName(@RequestBody SearchDto searchDto){
        /*searchDto.setAppUserId(getUserId());*/
        String authorization =   getRequest().getHeader("AUTHORIZATION");
        if (authorization!=null){
            searchDto.setAppUserId(Math.abs( Long.valueOf( authorization.split("\\_")[0] ) ));
        }
        return duoduoUserSpaceService.certfMyName(searchDto);
    }

    /**
     * 认证车位
     * @param searchDto
     * @return
     */
    @ResponseBody
    @RequestMapping("/certfMyCarpart")
    public ResultDto certfMyCarpart(@RequestBody SearchDto searchDto){
        /*searchDto.setAppUserId(getUserId());*/
        String authorization =   getRequest().getHeader("AUTHORIZATION");
        if (authorization!=null){
            searchDto.setAppUserId(Math.abs( Long.valueOf( authorization.split("\\_")[0] ) ));
        }
        return duoduoUserSpaceService.certfMyCarpart(searchDto);
    }




    /**
     * 我的车位
     * @param searchDto
     * @return
     */
    @ResponseBody
    @RequestMapping("/findMyParking")
    public ResultDto findMyParking(@RequestBody SearchDto searchDto){
        /*searchDto.setAppUserId(getUserId());*/
        String authorization =   getRequest().getHeader("AUTHORIZATION");
        if (authorization!=null){
            searchDto.setAppUserId(Math.abs( Long.valueOf( authorization.split("\\_")[0] ) ));
        }
        return duoduoUserSpaceService.findMyParking(searchDto);
    }

    /**
     * 发布我的车位
     * @param searchDto
     * @return
     */
    @ResponseBody
    @RequestMapping("/saleMyParking")
    public ResultDto saleMyParking(@RequestBody SearchDto searchDto){
        searchDto.setAppUserId(getUserId());
        return duoduoUserSpaceService.saleMyParking(searchDto);
    }

    /**
     * 取消出租
     * @param searchDto
     * @return
     */
    @ResponseBody
    @RequestMapping("/cancleSale")
    public ResultDto cancleSale(@RequestBody SearchDto searchDto){
        searchDto.setAppUserId(getUserId());
        return duoduoUserSpaceService.cancleSale(searchDto);
    }

    /**
     * 文件图片文件上传
     * */
    @PostMapping(value="/uploadImg")
    @ResponseBody
    public String uploadImg(MultipartHttpServletRequest muiltRequest, HttpServletRequest servletRequest){
        com.xm.xm.util.ResultDto resultDto = new com.xm.xm.util.ResultDto();
        try {
            String filename = "";//源文件名
            String path1="http://javaapi.huibotc.com/hppark/WEB-INF/classes/hppark/"; //返回的访问图片路径
//           String fileName = muiltRequest.getFileNames(); //&#x5f97;&#x5230;&#x6587;&#x4ef6;&#x540d;&#xff08;&#x6ce8;&#x610f;&#x3002;&#x662f;content-type &#x4e2d;&#x7684;name="file1"&#xff0c;&#x800c;&#x4e0d;&#x662f;&#x771f;&#x6b63;&#x7684;&#x6587;&#x4ef6;&#x540d;&#xff09;
            String fileName = muiltRequest.getFileNames().next(); //得到文件名（注意。是content-type 中的name="file1"，而不是真正的文件名）
            MultipartFile file = muiltRequest.getFile(fileName);   //得到该文件
            if (file.getOriginalFilename().length() > 0) {
                filename = new String(file.getOriginalFilename().getBytes("ISO-8859-1"),"UTF-8");//中文转码,得到源文件名
                System.out.print(fileName+"jjjjjjjjjjjjjjjjjjjj");
                //得到工程文件目录
               String path = this.getClass().getResource("/").getPath();
                path = path.replaceAll("(.)/\\..*", "$1") + servletRequest.getContextPath(); //到工程目录
                String filePath = path;
                File uploadFile = new File(filePath,     //建立本地文件
                        filename);
                if (!uploadFile.getParentFile().exists()) {
                     uploadFile.getParentFile().mkdirs();//如果是多级文件使用mkdirs();如果就一层级的话，可以使用mkdir()
                }
                InputStream inputStream = null;
                inputStream = file.getInputStream();
                FileUtils.copyInputStreamToFile(inputStream,  //把获取的文件存储到指定位置
                        uploadFile);

               /* filename = new String(filename.getBytes(),"ISO-8859-1");//转码返回*/
                File uploadFile1= new File(path1,     //建立本地文件
                        filename);
               String filename2 = new String(uploadFile1.toString().getBytes(),"ISO-8859-1");//转码返回
                resultDto.setData(filename2);
            }
        }catch (UnsupportedEncodingException e){
            resultDto.setCode(ErrorCode.C10012);
            resultDto.setMsg(ErrorCode.E10012);
            e.printStackTrace();
            return  resultDtoToJsonString(resultDto);
        }catch (IOException e){
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
            return  resultDtoToJsonString(resultDto);
        }
        resultDto.setMsg("上传成功");
        return  resultDtoToJsonString(resultDto);
    }

    /**
     * 车位审核
     * */
    @PostMapping(value="/savePicPath")
    @ResponseBody
    public ResultDto savePicPath(@RequestBody SearchDto searchDto){
        String authorization =   getRequest().getHeader("AUTHORIZATION");
        if (authorization!=null){
            searchDto.setAppUserId(Math.abs( Long.valueOf( authorization.split("\\_")[0] ) ));
        }
        return duoduoUserSpaceService.savePicPath(searchDto);
    }

}
